﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.baseset.sysrole
{
    public partial class rolegroup : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "select * from rulegroup order by sysdate";
            string error_msg = "";

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            //绑定菜单
            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("rulegroupid");
            dt.Columns.Add("rulegroupremark");
            dt.Columns.Add("stu1");
            dt.Columns.Add("sysdate");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["rulegroupid"] = dr["rulegroupid"].ToString();
                drnew["rulegroupremark"] = dr["rulegroupremark"].ToString();
                drnew["stu1"] = (dr["stu1"].ToString() == "0" ? "禁用":"启用");
                drnew["sysdate"] = dr["sysdate"].ToString();

                dt.Rows.Add(drnew);
            }

            rplist.DataSource = dt;
            rplist.DataBind();
        }

        private void delete(string id)
        {
            if(String.IsNullOrEmpty(id))
            {
                return;
            }

            alert_msg = "";

            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //删除rulemenu中数据
                sql = String.Format("delete from rulemenu where rulegroupid='{0}'", id);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                //删除rulegroup中数据
                sql = String.Format("delete from rulemenu where rulegroup='{0}'", id);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Delete);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        private void edit(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return;
            }

            Response.Redirect("/system/baseset/sysrole/rolegroup_edit.aspx?id=" + id);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }

        //查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bind();

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Search);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/baseset/sysrole/rolegroup_add.aspx");
        }

        //删除与编辑
        protected void rplist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "Delete":
                    {
                        delete(id);

                        break;
                    }
                case "Edit":
                    {
                        edit(id);

                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
}